/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2021-08-30 14:14:29
 * @LastEditors: zhl
 * @LastEditTime: 2021-09-30 17:12:24
 * @Description: file content
 */
#include <stdio.h>
#include <string.h>

#include "dlt698_main.h"
#include "dlt698_db.h"
#include "Parse_Packet.h"
#include "Server-APDU.h"
#include "CThreadNotify.h"
#include "dlt698_global.h"
#include "CLogger.h"
#include "CThreadPool.h"
#include "dbif.h"
#include "hex.h"
#include "common.h"


std::shared_ptr<CConfigManager> dlt698_global_conf;
//处理包信息
std::unordered_map<std::string, std::shared_ptr<MessageManager>> g_handler;



char *get_module_name(){

    return (char *)"3";//"DL/T698";
}
//

void create_handler(std::string addr){
    //
    if(!addr.empty()){
        auto handler = g_handler.find(addr);
        if(handler == g_handler.end()){
            std::shared_ptr<MessageManager> mp = std::make_shared<MessageManager>(addr, (uint8_t *)NULL, 0);
            
            g_handler.insert(std::make_pair(addr, mp));
        }
    }
}
/////////////////////////////////////////////////////////////////
/* 重新设计 */
int run(std::shared_ptr<CConfigManager> cm, 
        std::shared_ptr<SerialProxy> sp_ptr, 
        std::shared_ptr<CMosquittoManager> mm, 
        IModule &im,
        std::shared_ptr<TASK_PARAMS> task_params){
    if((NULL == cm) || (nullptr == sp_ptr) || (NULL == mm)){
        return -1;
    }
    if(dlt698_global_conf != cm){
        dlt698_global_conf = cm;
    }
    if(sp_ptr->c_if_addr.empty()){
        CLogger::getInstance()->LOG_W("DL/T698调用接口run方法时，接口地址为空。");
        return -1;
    }
    auto handler = g_handler.find(sp_ptr->c_if_addr);
    if(handler == g_handler.end()){
        create_handler(sp_ptr->c_if_addr);
    }
    handler = g_handler.find(sp_ptr->c_if_addr);
    if(handler != g_handler.end()){
        handler->second->run(cm, sp_ptr.get(), mm, task_params.get());
    } 
    return 0;
}

int packet_parent_frame(std::shared_ptr<CConfigManager> cm, 
                        std::shared_ptr<TERMINAL_DEVICE> pdev, 
                        uint8_t *data, int &len){
    return len;
}

int parse_parent_frame(std::shared_ptr<CConfigManager> cm, uint8_t *data, int &len){
    return len;
}